package com.cloudwise.thread;

/**
 * @author ：Teacher陈
 * @date ：Created in 2021/1/6 15:28
 * @description：sleep和wait的区别
 * @modified By：
 * @version: 1
 */
public class DiffSleepWait {

    public static void main(String[] args) {
        //创建一个锁对象
        final Object lockObj = new Object();
        //创建两个线程
        Runnable t1 = new Runnable() {
            @Override
            public void run() {
                synchronized (lockObj) {
                    System.out.println(1);
                    try {
                       /* Thread.sleep(1000);*/
                        //破坏锁
                        lockObj.wait(1000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    System.out.println(2);
                }
            }
        };
        Runnable t2 = new Runnable() {
            @Override
            public void run() {
                synchronized (lockObj) {
                    System.out.println(3);
                    try {
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    System.out.println(4);
                }
            }
        };

        new Thread(t1).start();
        new Thread(t2).start();
    }
}
